$(function(){
    obj.init();
});

var obj = new function(){
    'use strict';
    return {
        dataGrid: null,
        params: {
            year: null,
            province: null,
            city: null,
            depart: null
        },
        dom: {
            // mode: null,
            search: null,
            year: null,
            depart: null,
            province: null,
            city: null,
            // startTime: null,
            // endTime: null
        },
        init: function () {
            this.dom.search     = $('#bt_list_search');
            this.dom.mode       = $('#list_mode');
            this.dom.year = $('#list_year');
            this.dom.depart = $('#list_depart');
            this.dom.province = $('#select_province');
            this.dom.city = $('#select_city');
            // this.dom.startTime  = $('#list_startTime');
            // this.dom.endTime    = $('#list_endTime');

            this.dataGrid = new xhDataGrid($('#list'), 'cost/getSchoolCharge');
            this.dataGrid.parseToolbar();//.activeBtn(null, $('#bt_list_jump'));
            this.funObj().dataGridEvent();

            this.listener();
        },
        listener: function () {
            var $this = this;
            var $length = 0;
            $('#select_province').combobox({
                onSelect:function (record) {
                    if(record.value != '') {
                        $('#select_city').combobox({
                            url: baseUrl + 'customer/customer/getAreaById/' + record.value,
                            valueField: 'areaCode',
                            textField: 'areaName',
                            onSelect: function (record2) {
                                if(record2.areaCode != ''){
                                    $('#select_county').combobox({
                                        url: baseUrl + 'customer/customer/getAreaById/' + record2.areaCode,
                                        valueField: 'areaCode',
                                        textField: 'areaName'
                                    })
                                }else {
                                    $('#select_county').combobox('setValue','');
                                }
                            }
                        })
                    }else {
                        $('#select_city').combobox('setValue','');
                        $('#select_county').combobox('setValue','');
                    }
                }
            });
            //运营区域
            $('#list_depart').combotree({
                url: '/Device/getDepData',
                animate:true,
                onLoadSuccess:function(node,data){
                    $length = data.length;
                    $.each(data,function (id,value) {
                        $length = $length + value.children.length;
                    })
                },
                onShowPanel:function(){
                    if ($length <= 8){
                        $(this).combobox('panel').height("auto");//高度自适应
                    }else{
                        $(this).combobox('panel').height("200");//高度自定义
                    }
                },
                onBeforeSelect: function (node) {
                    var t = $(this).tree;
                    var isLeaf = t('isLeaf', node.target);
                    if (!isLeaf) {//选择的不是叶子节点
                        return false;
                    }
                }
            });
            //查询
            $this.dom.search.click(function () {
                $this.funObj().search();
            }).click();
            xhTool.bindDocKeyDown($this.dom.search);
            //切换查询模式
            // $this.funObj().setSearchStatus(1);
            // $this.dom.mode.combobox({
            //     onChange: function (newValue, oldValue) {
            //         $this.funObj().setSearchStatus(newValue);
            //     }
            // });
            //跳转
            // $('#bt_list_jump').click(function () {
            //     if (xhExtra.buttonDisabled($(this))) return false;
            //     $this.funObj().jumpStudCharge();
            // });
            //导出
            $('#bt_list_export').click(function () {
                $this.funObj().exportSchoolCharge();
            });
        },
        funObj: function(){
            var $this = this;
            return {
                search: function () {
                    $this.params = {
                        // mode: xhExtra.getValue($this.dom.mode),
                        year: xhExtra.getValue($this.dom.year),
                        // startTime: xhExtra.getValue($this.dom.startTime),
                        // endTime: xhExtra.getValue($this.dom.endTime)
                        province: xhExtra.getValue($this.dom.province),
                        city: xhExtra.getValue($this.dom.city),
                        depart: xhExtra.getValue($this.dom.depart),
                    };
                    $this.dataGrid.reload($this.params);
                },
                dataGridEvent: function () {
                    $this.dataGrid.onDblClickRow = function (rowIndex, rowData) {
                        $this.funObj().showCostDetail(rowData.iSchoolId, rowData.sSchoolName);
                    };
                },
                // setSearchStatus: function (value) {
                //     if (parseInt(value) === 1) {
                //         $this.dom.year.combobox('enable');
                //         $this.dom.startTime.add($this.dom.endTime).datebox('disable');
                //     }
                //     else {
                //         $this.dom.year.combobox('disable');
                //         $this.dom.startTime.add($this.dom.endTime).datebox('enable');
                //     }
                // },
                showCostDetail: function (schoolId, schoolName) {
                    window.top.xhTop.showModal({
                        title: '明细-' + schoolName,
                        content: xhTool.getFrameString(
                            'cost/schoolChargeDetail',
                            // $.extend({}, {schoolId: schoolId}, $this.funObj().getValidParams()),
                            {schoolId: schoolId, year: $this.params.year},
                            400
                        ),
                        size: 'lg'
                    });
                },
                // jumpStudCharge: function () {
                //     var params = $.extend({},
                //         {
                //             schoolId: $this.dataGrid.getSelectValue('iSchoolId'),
                //             schoolName: encodeURI($this.dataGrid.getSelectValue('sSchoolName'))
                //         },
                //         $this.funObj().getValidParams()
                //     );
                //     window.top.xhTop.createNewTab('收费管理', 'cost/studCharge?' + xhTool.parseParam(params));
                // },
                // getValidParams: function () {
                //     return parseInt($this.params.mode) === 1
                //         ? {year: $this.params.year}
                //         : {startTime: $this.params.startTime, endTime: $this.params.endTime};
                // },
                exportSchoolCharge: function () {
                    $this.funObj().search();
                    window.location.href = baseUrl + 'cost/exportSchoolCharge?' + xhTool.parseParam($this.params);
                }
            }
        }
    }
}

